<?php

namespace backend\controllers;

use Yii;
use common\models\Setting\Setting;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\UploadedFile;

use backend\controllers\BackendController;

use zhongkefu\helpers\SwfiEmail;
use zhongkefu\helpers\Thumb;
use zhongkefu\helpers\Utils;
use common\models\adminlog\AdminLog;

class SettingController extends BackendController
{

    public function actionIndex()
    {
        if(Yii::$app->request->isPost)
        {

			$setting = Yii::$app->request->post('Setting');
            foreach($setting as $key => $value) {
                Setting::updateAll(['value' => $value], ['code' => $key]);
			}
             $siteRoot =  Yii::$app->params['setimg'];
			//var_dump($_FILES);die;
			 if (!empty($_FILES)) {
                if($_FILES['Setting']['tmp_name']['imglogo']){
					$tempPath = $_FILES['Setting']['tmp_name']['imglogo'];
					$filesName = uniqid() . '.' . pathinfo($_FILES['Setting']['name']['imglogo'], PATHINFO_EXTENSION);
					$uploadPath = $siteRoot . $filesName;
					move_uploaded_file($tempPath, $uploadPath);

					Setting::updateAll(['value' => $filesName], ['code' => 'imgLOGO']);
				}
				if($_FILES['Setting']['tmp_name']['dizhiimg']){
					$tempPath = $_FILES['Setting']['tmp_name']['dizhiimg'];
					$filesName = uniqid() . '.' . pathinfo($_FILES['Setting']['name']['dizhiimg'], PATHINFO_EXTENSION);

					$uploadPath = $siteRoot . $filesName;
					move_uploaded_file($tempPath, $uploadPath);
					Setting::updateAll(['value' => $filesName], ['code' => 'dizhiimg']);
				}
				if($_FILES['Setting']['tmp_name']['wate_pic']){
					$tempPath = $_FILES['Setting']['tmp_name']['wate_pic'];
					$filesName = uniqid() . '.' . pathinfo($_FILES['Setting']['name']['wate_pic'], PATHINFO_EXTENSION);

					$uploadPath = $siteRoot . $filesName;
					move_uploaded_file($tempPath, $uploadPath);
					Setting::updateAll(['value' => $filesName], ['code' => 'wate_pic']);
				}
				if($_FILES['Setting']['tmp_name']['wbimg']){
					$tempPath = $_FILES['Setting']['tmp_name']['wbimg'];
					$filesName = uniqid() . '.' . pathinfo($_FILES['Setting']['name']['wbimg'], PATHINFO_EXTENSION);

					$uploadPath = $siteRoot . $filesName;
					move_uploaded_file($tempPath, $uploadPath);
					Setting::updateAll(['value' => $filesName], ['code' => 'wb_chat']);
				}
				if($_FILES['Setting']['tmp_name']['wangimg']){
					$tempPath = $_FILES['Setting']['tmp_name']['wangimg'];
					$filesName = uniqid() . '.' . pathinfo($_FILES['Setting']['name']['wangimg'], PATHINFO_EXTENSION);

					$uploadPath = $siteRoot . $filesName;
					move_uploaded_file($tempPath, $uploadPath);
					Setting::updateAll(['value' => $filesName], ['code' => 'wang']);
				}
				if($_FILES['Setting']['tmp_name']['moimg']){
					$tempPath = $_FILES['Setting']['tmp_name']['moimg'];
					$filesName = uniqid() . '.' . pathinfo($_FILES['Setting']['name']['moimg'], PATHINFO_EXTENSION);

					$uploadPath = $siteRoot . $filesName;
					move_uploaded_file($tempPath, $uploadPath);
					Setting::updateAll(['value' => $filesName], ['code' => 'mobile']);
				}
				
            }
			Adminlog::addlog(yii::$app->user->id, "修改网站设置");
        }
        //$settingParent = Setting::find()->where(['parent_id' => 0])->orderBy(['sort_order' => SORT_ASC])->all();
		$settingParent = Setting::findBySql('SELECT * FROM setting where parent_id=0 and id not in(51,52,53,54) order by sort_order asc')->all();
        return $this->render('index', [
            'settingParent' => $settingParent,
        ]);
    }
	
	public function actionMsend(){
		$mailer = SwfiEmail::getEmail();
		$mail= $mailer->compose();
        $mail->setTo('284732989@qq.com');
        $mail->setSubject("Test title1231");
        $mail->setTextBody('Test conten1231123t');
        if($mail->send()){
			yii::$app->getSession()->setFlash('success','发送成功!');
		}else{
			yii::$app->getSession()->setFlash('error','发送失败！');
		}
		return $this->redirect(['setting']);
	}
	//zsn 清楚缓存 20131230
	public function actionDelcache(){
		$cache_delet1 = 2;
		$cache_delet2 = 2;
		$cache_delet3 = 2;
		if(file_exists(Yii::$app->params['frontcache']))
			$cache_delet1 = Utils::deldir(Yii::$app->params['frontcache']);
		if(file_exists(Yii::$app->params['backcache']))
			$cache_delet2 = Utils::deldir(Yii::$app->params['backcache']);
		if(file_exists(Yii::$app->params['mobilecache']))
			$cache_delet3 = Utils::deldir(Yii::$app->params['mobilecache']);

		if(!$cache_delet1){
			echo "<script>
						alert('前台缓存清除失败！');
				  </script>";
		}
		if(!$cache_delet2){
			echo "<script>
						alert('后台缓存清除失败！');
				  </script>";
		}
		if(!$cache_delet3){
			echo "<script>
						alert('手机端缓存清除失败！');
				  </script>";
		}
		if($cache_delet1&&$cache_delet2&&$cache_delet3){
			echo "<script>
						alert('缓存清除完成！');
				  </script>";
		}
		$settingParent = Setting::findBySql('SELECT * FROM setting where parent_id=0 and id not in(51,52,53,54) order by sort_order asc')->all();
		return $this->render('index', [
            'settingParent' => $settingParent,
        ]);
	}

}
